package kku.cs.fgl.transition;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

public class Fade extends TransitionBase {
	public static final int IN = 0;
	public static final int OUT = 1;
	private int fademode = 0;
	protected float alpha1=0,alpha2=1;
	private Color fadeColor = null;

	public Fade() {
		this(1000,1);
	}
	public Fade(int duration) {
		this(duration,1);
	}

	public Fade(int duration,int fademod) {
		super(duration);
		this.fademode = fademod;
	}

	public Fade(int duration, int fademode, Color fadeColor) {
		super(duration);
		this.fademode = fademode;
		this.fadeColor = fadeColor;
	}
	public Fade(int duration,Color fadeColor) {
		this(duration,1,fadeColor);
	}
	
	void init(StateBasedGame game, GameContainer container) {
        if(fademode==1){
        	alpha1=1;
        	alpha2=0;
        }
        if(fadeColor!=null && scene2!=null)
		 scene2.setVisible(false);
	}
	
	public void postRender(StateBasedGame game, GameContainer container, Graphics g) throws SlickException {
		if(fadeColor!=null && getTime()>0){
		  g.setColor(fadeColor);
		  g.fillRect(0,0,container.getWidth(),container.getHeight());
		}
		super.postRender(game, container, g);
	}
	public void update(StateBasedGame game, GameContainer container, int delta) throws SlickException {
		super.update(game, container, delta);
		float t = (float) getTime() / getDuration();
		float a=alpha1+(alpha2-alpha1)*t;
        if(scene1!=null){
  		   scene1.setAlphaLevel(a);
        }
	}
	public Color getFadeColor() {
		return fadeColor;
	}
	public void setFadeColor(Color fadeColor) {
		this.fadeColor = fadeColor;
	}
	public int getFademode() {
		return fademode;
	}
	public void setFademode(int fademode) {
		this.fademode = fademode;
	}
	
}
